package agents.behaviours;

import jade.core.behaviours.SimpleBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class SubscriptionDataServiceBehaviour extends SimpleBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String attribute;

	public SubscriptionDataServiceBehaviour(String attribute){
		this.attribute = attribute;
	}
	
	@Override
	public void action() {
		ACLMessage msg = myAgent.blockingReceive(MessageTemplate.MatchSender(myAgent.getDefaultDF()));
		
		try {
			DFAgentDescription[] result = DFService.decodeNotification(msg.getContent());
			if(result.length>0){
				ACLMessage infMsg = new ACLMessage(ACLMessage.INFORM);
				infMsg.addReceiver(result[0].getName());
				infMsg.setContent(attribute);
				infMsg.setProtocol(FIPANames.InteractionProtocol.FIPA_QUERY);
				myAgent.send(infMsg);
				ACLMessage reply = myAgent.blockingReceive();
				System.out.println(reply);
			}
		} catch (FIPAException e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public boolean done() {
		// TODO Auto-generated method stub
		return false;
	}

}
